package com.yuncheng.oaApi.FlowController;

import com.yuncheng.entity.YcSysUser;
import com.yuncheng.oaApi.constant.OaCommonFlowConstant;
import com.yuncheng.oaApi.constant.NkConstant;
import com.yuncheng.oaApi.constant.OaFlowConstant;
import com.yuncheng.oaApi.constant.OaSpcyJcyFybxdConstant;
import com.yuncheng.oaApi.vo.*;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.oaApi.FlowService.NewNkRsJcyBxFlowService;
import com.yuncheng.annotation.AutoLog;
import com.yuncheng.oaApi.busService.MyLcFlowService;
import com.yuncheng.oaApi.entity.*;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.oaApi.mapper.OaSpcyJcyFybxdMapper;
import com.yuncheng.oaApi.mapper.OaSysRoleMapper;
import com.yuncheng.oaApi.service.*;
import com.yuncheng.spcyApi.utils.DateConvert;
import com.yuncheng.oaApi.utils.GetUuIdUtils;
import com.yuncheng.utils.ThreadContextHelper;
import com.yuncheng.vo.*;
import groovy.lang.Lazy;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import utils.DateConversionUtils;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

@Api(tags = "NEW_JCY-报销申请流程")
@RestController
@RequestMapping("/api/spzx-oa/nk-jcybx")
public class NewNkRsJcyBxFlowController {
    private static final Logger log = LoggerFactory.getLogger(NewNkRsJcyBxFlowController.class);

    @Resource
    private IOaSpcySqsxJobService spcySqsxJobService;

    @Resource
    private IOaYcSysUserService ycSysUserService;

    @Resource
    @Lazy
    private IOaSysTzMessageService sysTzMessageService;

    @Resource
    @Lazy
    private MyLcFlowService myLcFlowService;

    @Resource
    @Lazy
    private IOaProcessBusinessService oaProcessBusinessService;



    @Resource
    @Lazy
    private NewNkRsJcyBxFlowService newNkRsJcyBxFlowService;



    @Resource
    @Lazy
    private INkRsXmyszbbService nkRsXmyszbbService;

    @Resource
    @Lazy
    private IOaSpcyJcyFybxdService spcyJcyFybxdService;

    @Resource
    @Lazy
    private OaSpcyJcyFybxdMapper oaSpcyJcyFybxdMapper;

    @Resource
    @Lazy
    private IOaSysRoleService sysRoleService;

    @Resource
    @Lazy
    private OaSysRoleMapper oaSysRoleMapper;


    @Resource
    @Lazy
    private IOaSysFlowTempleService sysFlowTempleService;

    @Resource
    @Lazy
    private IOaSysFlowNodeService sysFlowNodeService;



    /**
     * 通过事项id 判断报销是否提交流程
     */
    @AutoLog(value = "通过事项id 判断报销是否提交流程")
    @ApiOperation(value = "通过事项id 判断报销是否提交流程")
    @GetMapping("getLczt")
    public HttpResult getLczt(String sqsxid){
        List<OaSpcyJcyFybxd> oaSpcyJcyFybxds = spcyJcyFybxdService.list(new LambdaQueryWrapper<OaSpcyJcyFybxd>()
                .eq(OaSpcyJcyFybxd::getFsqsxid, sqsxid));

        if (CollectionUtils.isNotEmpty(oaSpcyJcyFybxds)){
            String fsffqlc = oaSpcyJcyFybxds.get(0).getFsffqlc();
            if (fsffqlc.equals(OaCommonFlowConstant.SFFQLC_YES)){
                return HttpResult.ok(true);
            }else {
                return HttpResult.ok(false);
            }
        }
        return HttpResult.ok(false);
    }




    /**
     * 流程列表分页查询
     * @param
     * @param pageNo
     * @param pageSize
     * @param onlyCurrentUser
     * @return
     */
    @AutoLog(value = "流程监控-分页查询")
    @ApiOperation(value = "流程监控-分页查询")
    @GetMapping("flowJkPage")
    public HttpResult flowJkPage(
            SpcyJcyBxdVo spcyJcyBxdVo,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "onlyCurrentUser", defaultValue = "false") boolean onlyCurrentUser) {
        CurrentUserVo user = ThreadContextHelper.getCurrentUser();
        if (user == null) {
            return HttpResult.error("请重新登录，Token已失效！");
        }
        spcyJcyBxdVo.setFlcmc(OaFlowConstant.NK_BXSQLC_NAME);
        Page<SpcyJcyBxdVo> page = new Page<>(pageNo, pageSize);

        IPage<SpcyJcyBxdVo> list = null;
        if (StringUtils.isNotBlank(spcyJcyBxdVo.getOaid())){
            OaSpcyJcyFybxd oaSpcyJcyFybxd = spcyJcyFybxdService.getById(spcyJcyBxdVo.getOaid());
            if (oaSpcyJcyFybxd != null){
                list = oaSpcyJcyFybxdMapper.flowJkPage(page,onlyCurrentUser,user,spcyJcyBxdVo);
            }
        }
        return HttpResult.ok(list);
    }

    /**
     * 流程列表分页查询
     * @param
     * @param pageNo
     * @param pageSize
     * @param onlyCurrentUser
     * @return
     */
    @AutoLog(value = "流程列表-分页查询")
    @ApiOperation(value = "流程列表-分页查询")
    @GetMapping("flowJobPage")
    public HttpResult<?> flowJobPage(
            SpcyJcyBxdVo spcyJcyBxdVo,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "onlyCurrentUser", defaultValue = "true") boolean onlyCurrentUser) {
        CurrentUserVo user = ThreadContextHelper.getCurrentUser();
        if (user == null) {
            return HttpResult.error("请重新登录，Token已失效！");
        }
        spcyJcyBxdVo.setFlcmc(OaFlowConstant.NK_BXSQLC_NAME);
        if (StringUtils.isBlank(spcyJcyBxdVo.getFblzt())) {
            spcyJcyBxdVo.setFblzt(OaCommonFlowConstant.BLZT_DB);
        }
        Page<SpcyJcyBxdVo> page = new Page<>(pageNo, pageSize);
        IPage<SpcyJcyBxdVo> list = oaSpcyJcyFybxdMapper.flowJobPage(page, onlyCurrentUser, user, spcyJcyBxdVo);
        return HttpResult.ok(list);
    }


    /**
     * 我的已办
     */
    @AutoLog(value = "流程列表-我的已办")
    @ApiOperation(value = "流程列表-我的已办")
    @GetMapping("myJobPage")
    public HttpResult<?> myJobPage(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            SpcyJcyBxdVo spcyJcyBxdVo) {
        CurrentUserVo user = ThreadContextHelper.getCurrentUser();
        if (user == null) {
            return HttpResult.error("请重新登录，Token已失效！");
        }
        Page<SpcyJcyBxdVo> page = new Page<>(pageNo, pageSize);
        IPage<SpcyJcyBxdVo> list = oaSpcyJcyFybxdMapper.myYbFlowPage(page, true, user, spcyJcyBxdVo);
        return HttpResult.ok(list);
    }



    /**
     * 数据修订
     */
    @AutoLog(value = "列表-数据修订")
    @ApiOperation(value = "列表-数据修订")
    @GetMapping("Pagesjxd")
    public HttpResult<?> Pagesjxd(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            SpcyJcyBxdVo spcyJcyBxdVo) {

        CurrentUserVo user = ThreadContextHelper.getCurrentUser();
        if (user == null) {
            return HttpResult.error("请重新登录，Token已失效！");
        }

        Page<SpcyJcyBxdVo> page = new Page<>(pageNo, pageSize);
        IPage<SpcyJcyBxdVo> list = oaSpcyJcyFybxdMapper.myYbFlowPage(page, false, user, spcyJcyBxdVo);
        return HttpResult.ok(list);
    }




    @AutoLog(value = "发起报销审批流程")
    @ApiOperation(value = "发起报销审批流程")
    @PostMapping(value = "initBzFlow")
    public HttpResult<?> initBzFlow(@RequestBody OaSpcyJcyFybxd oaSpcyJcyFybxd) {

        String oaid = "";
        //当前人的id
        CurrentUserVo currentUser = ThreadContextHelper.getCurrentUser();
        if (currentUser == null) {
            return HttpResult.error("当前登录已失效，请重新登录");
        }
        if (StringUtils.isNotBlank(oaSpcyJcyFybxd.getId())){
            OaSpcyJcyFybxd nkids = spcyJcyFybxdService.getById(oaSpcyJcyFybxd.getId());
            if (!nkids.getFbxdzt().equals(OaCommonFlowConstant.BXDZT_WC)){
                throw new BusinessException("请先完成报销单填写");
            }

            ConvertUtils.register(new DateConvert(), Date.class);
            try {
                BeanUtils.populate(nkids, BeanUtils.describe(oaSpcyJcyFybxd));
            } catch (IllegalAccessException e) {
                log.error("出错-保存事项【非法访问异常】,{}", e.getMessage());
            } catch (InvocationTargetException e) {
                log.error("出错-保存事项【调用TargetException】,{}", e.getMessage());
            } catch (NoSuchMethodException e) {
                log.error("出错-找不到可用方法", e.getMessage());
            }
            oaid = oaSpcyJcyFybxd.getId();

            // Date日期单独做处理
            oaSpcyJcyFybxd.setFlczt(OaCommonFlowConstant.FLOW_LCZB);
//            if (nkids.getFbxdlx().equals(NkConstant.FYBXD_JCYBXD)){
//                spcyJcyFybxd.setFbxdzt(SpcyJcyFybxdConstant.STATE_WC);
//            }

            spcyJcyFybxdService.updateById(nkids);
        }else {
            oaid = GetUuIdUtils.ReplaceUuId();



            oaSpcyJcyFybxd.setFlczt(OaCommonFlowConstant.FLOW_LCZB);

            spcyJcyFybxdService.save(oaSpcyJcyFybxd);
        }

        /**
         * @Author: lhb
         * @Description : 是否发起流程   如果是开始申请就跳转流程
         *               在这里设置  指定下个环节节点 做当前跳转流程的判断，然后设置变量接收环节的值，写到nextNodeName中
         * @DateTime:
         * @Params: Fsffqlc
         */
        String msg = "保存成功";
        String lcmbCode = OaFlowConstant.NK_BXSQLC;

        OaJobEntity job = new OaJobEntity();
        // 初始化流程
        Result result = newNkRsJcyBxFlowService.createProcess(lcmbCode,oaid, currentUser);
        //做一次规则校验 如果查到此oaid 在表中已经插入 那就执行更新
        OaSpcyJcyFybxd nkRsBxlcServiceById = spcyJcyFybxdService.getById(oaid);
        List<OaProcessBusiness> oaProcessBusinessList = oaProcessBusinessService.list(new LambdaQueryWrapper<OaProcessBusiness>()
                .eq(OaProcessBusiness::getFpid, oaid));
        String bt = "";
        String zy = "";
        if (nkRsBxlcServiceById.getFbxdlx().equals(NkConstant.FYBXD_JCYBXD)){

             bt = nkRsBxlcServiceById.getFjbr()+"提交的报销申请"; //标题
             zy = "申请日期:["+ DateConversionUtils.DateToYMDHMSChineseString(new Date()) +"]"+"\n"
                    +"摘要:["+ nkRsBxlcServiceById.getFbxnrms() + "]"+"\n";
        }else {
            bt = currentUser.getName()+"提交的报销申请"; //标题
            zy = "申请日期:["+ DateConversionUtils.DateToYMDHMSChineseString(nkRsBxlcServiceById.getFtbrq()) +"]"+"\n"
                    +"摘要:["+ nkRsBxlcServiceById.getFbxnrms() + "]"+"\n";
        }



        if (oaProcessBusinessList.size() == 0){
            //初始化的时候 像 流程业务主表添加拼接初始化数据
            oaProcessBusinessService.addProcessBx(oaid,zy,bt,lcmbCode,OaFlowConstant.NK);
        }else {
            String id = oaProcessBusinessList.get(0).getId();  //当前已经插入的主键id 然后更新 摘要信息
            OaProcessBusiness oaProcessBusiness = oaProcessBusinessService.getById(id);
            oaProcessBusiness.setFzy(zy);
            oaProcessBusinessService.updateById(oaProcessBusiness);
        }

        if (!result.isSuccess()){
            return HttpResult.error(result.getMessage());
        }else {
            job = (OaJobEntity)result.getResult();
            if (job != null) {
                // 是否提交流程
                if(StringUtils.isBlank(oaid)){
                    return HttpResult.error("参数丢失");
                }
                String nextNodeId = "";
                String nextNodeDbrId = "";
                if (StringUtils.isNotBlank(oaSpcyJcyFybxd.getFsffqlc()) && oaSpcyJcyFybxd.getFsffqlc().equals(OaCommonFlowConstant.SFFQLC_YES)) {

                    if (StringUtils.isBlank(oaSpcyJcyFybxd.getFlczt())) {
                        oaSpcyJcyFybxd.setFlczt(OaCommonFlowConstant.FLOW_LCZB);
                    }
                    //如果是在职人员或者 对公对私报销 流程 不走经办人审核环节,直接到科室审核环节
                        //使用流程编码查询流程模板
                        List<OaSysFlowTemple> templeList = sysFlowTempleService.list(new LambdaQueryWrapper<OaSysFlowTemple>().eq(OaSysFlowTemple::getFlcbm, lcmbCode));
                        String templeId = templeList.get(0).getId();

                        List<OaSysFlowNode> bmshNodeList = sysFlowNodeService.list(new LambdaQueryWrapper<OaSysFlowNode>().eq(OaSysFlowNode::getFlcmbid, templeId)
                                .eq(OaSysFlowNode::getFjdmc, OaFlowConstant.SPCYBXSQLC_HJMC_BMSH));

                        String bmshid = bmshNodeList.get(0).getId();//部门审核id



                    OaSpcyJcyFybxd oaSpcyJcyFybxd1 = spcyJcyFybxdService.getById(oaid);
                    String fbxdlx = oaSpcyJcyFybxd1.getFbxdlx();
                    if (StringUtils.isNotBlank(fbxdlx) && fbxdlx.equals(NkConstant.FYBXD_ZCBXD)
                            ||fbxdlx.equals(NkConstant.FYBXD_ZZRYCCBXD)
                            || fbxdlx.equals(NkConstant.FYBXD_DGZZSQD)){
                        nextNodeId = bmshid;
                    }else {
                        nextNodeId = null;
                    }



                    Result result1 = newNkRsJcyBxFlowService.passJob(oaid,job.getId(),"同意", nextNodeId, nextNodeDbrId);
                    if (!result1.isSuccess()) {
                        return HttpResult.error(result.getMessage());
                    }
                    msg = "提交成功";
                    //提交完成后 更新通知信息
                    sysTzMessageService.tjMessage(oaid);

                    List<OaProcessBusiness> oaProcessBusinessList1 = oaProcessBusinessService.list(new LambdaQueryWrapper<OaProcessBusiness>()
                            .eq(OaProcessBusiness::getFpid, oaid));
                    OaProcessBusiness oaProcessBusiness = oaProcessBusinessList1.get(0);
                    OaProcessBusiness byId1 = oaProcessBusinessService.getById(oaProcessBusiness.getId());
                    byId1.setFsfkch(0);
                    byId1.setFcshdb(OaFlowConstant.FLOW_BLZT_YB);
                    oaProcessBusinessService.updateById(byId1);

                    myLcFlowService.recall(oaid);
                }
            }
        }

        HashMap map = new HashMap();
        map.put("msg",msg);
        map.put("nkMap", oaSpcyJcyFybxd);
        map.put("jobMap",job);

        return HttpResult.ok(map);
    }

    /**
     * 内控-报销申请流程保存接口
     */
    @AutoLog(value = "内控-报销申请流程保存接口")
    @ApiOperation(value = "内控-报销申请流程保存接口")
    @PostMapping(value = "/saveFlow")
    public HttpResult<?> saveFlow(@RequestBody OaFlowVo oaFlowVo) {
        String submitType = OaCommonFlowConstant.SUBMIT_TYPE_SAVE; // 保存

        String jobId = oaFlowVo.getJobid(); // 当前流程任务id
        String oaid = oaFlowVo.getOaid(); // 当前办公主键id
        String opinion = oaFlowVo.getOpinion(); // 办理意见

        if (StringUtils.isBlank(oaFlowVo.getLcmbCode())){
            oaFlowVo.setLcmbCode(OaFlowConstant.NK_BXSQLC);
        }
        if (StringUtils.isBlank(jobId)){
            return HttpResult.error("流程任务id参数丢失，请联系管理员");
        }
        // 流程处理
        Result httpResult = newNkRsJcyBxFlowService.handleFlow(oaFlowVo, null, jobId, oaid, submitType, opinion);
        if (!httpResult.isSuccess()){
            return HttpResult.error(httpResult.getMessage());
        }
        Map map = new HashMap();
        HttpResult<Object> res = new HttpResult<>(true);
        res.setMessage("保存成功");
        res.setResult(map);
        return res;
    }

    @AutoLog(value = "内控-根据项目预算指标id查询")
    @ApiOperation(value = "内控-根据项目预算指标id查询")
    @GetMapping(value = "/selectxmyszb")
    public HttpResult<?> selectxmyszb(String xmyszbid){
        List<NkRsXmyszbb> list = new ArrayList();
        if (StringUtils.isNotBlank(xmyszbid)){
            list = nkRsXmyszbbService.list(new LambdaQueryWrapper<NkRsXmyszbb>().eq(NkRsXmyszbb::getId, xmyszbid));
        }

        return HttpResult.ok(list);
    }


    @AutoLog(value = "内控-报销申请流程提交接口")
    @ApiOperation(value = "内控-报销申请流程提交接口")
    @PostMapping(value = "/submitFlow")
    public HttpResult<?> submitFlow(@RequestBody OaFlowVo oaFlowVo) {

        String submitType = OaCommonFlowConstant.SUBMIT_TYPE_SUBMIT; // 提交

        String jobId = oaFlowVo.getJobid();
        String oaid = oaFlowVo.getOaid();
        String opinion = oaFlowVo.getOpinion();
        OaSpcyJcyFybxd oaSpcyJcyFybxd = spcyJcyFybxdService.getById(oaid);
        if (!oaSpcyJcyFybxd.getFbxdzt().equals(OaCommonFlowConstant.BXDZT_WC)){
            throw new BusinessException("未提交报销单，请先完成报销单填写");
        }


        OaJobEntity jobid = spcySqsxJobService.getById(jobId);
        if (StringUtils.isBlank(oaFlowVo.getLcmbCode())){
            oaFlowVo.setLcmbCode(OaFlowConstant.NK_BXSQLC);
        }

        if (jobid.getFhjmc().equals(OaFlowConstant.SPCYBXSQLC_HJMC_JBRSH)){

            String fysxmid = oaSpcyJcyFybxd.getFysxmid();
            if (StringUtils.isBlank(fysxmid)){
                throw new BusinessException("没有选择报销明细");
            }

        }

        if (jobid.getFhjmc().equals(OaFlowConstant.SPCYBXSQLC_HJMC_TXBXD)) {
            String lcmbCode = OaFlowConstant.NK_BXSQLC;

            List<OaSysFlowTemple> templeList = sysFlowTempleService.list(new LambdaQueryWrapper<OaSysFlowTemple>().eq(OaSysFlowTemple::getFlcbm, lcmbCode));
            String templeId = templeList.get(0).getId();


            List<OaSysFlowNode> bmshNodeList = sysFlowNodeService.list(new LambdaQueryWrapper<OaSysFlowNode>().eq(OaSysFlowNode::getFlcmbid, templeId)
                    .eq(OaSysFlowNode::getFjdmc, OaFlowConstant.SPCYBXSQLC_HJMC_BMSH));

            String bmshid = bmshNodeList.get(0).getId();//部门审核id


            if (StringUtils.isNotBlank(oaSpcyJcyFybxd.getFbxdlx())
                    && oaSpcyJcyFybxd.getFbxdlx().equals(NkConstant.FYBXD_ZCBXD)
                    || oaSpcyJcyFybxd.getFbxdlx().equals(NkConstant.FYBXD_ZZRYCCBXD)
                    || oaSpcyJcyFybxd.getFbxdlx().equals(NkConstant.FYBXD_DGZZSQD)) {
                oaFlowVo.setNextNodeId(bmshid);
            } else {
                oaFlowVo.setNextNodeId("");
            }


        }



        if (jobid.getFhjmc().equals(OaFlowConstant.SPCYBXSQLC_HJMC_CNSH)) {

        } else {
            oaSpcyJcyFybxd.setFlczt(OaCommonFlowConstant.FLOW_LCZB);
        }
        if (StringUtils.isBlank(oaSpcyJcyFybxd.getFsffqlc())) {
            oaSpcyJcyFybxd.setFsffqlc(OaCommonFlowConstant.SFFQLC_YES);
        }

        spcyJcyFybxdService.updateById(oaSpcyJcyFybxd);

        if (StringUtils.isBlank(jobId)) {
            return HttpResult.error("流程任务id参数丢失，请联系管理员");
        }
        /* if (StringUtils.isBlank(opinion)){
            return HttpResult.error("请填写办理意见");2
        }*/
        OaJobEntity nowJob = null;
        Result httpResult1 = newNkRsJcyBxFlowService.handleFlow(oaFlowVo, nowJob, jobId, oaid, submitType, opinion);
        if (!httpResult1.isSuccess()){
            return HttpResult.error(httpResult1.getMessage());
        }
        List<OaProcessBusiness> oaProcessBusinessList1 = oaProcessBusinessService.list(new LambdaQueryWrapper<OaProcessBusiness>()
                .eq(OaProcessBusiness::getFpid, oaid));
        OaProcessBusiness oaProcessBusiness = oaProcessBusinessList1.get(0);
        OaProcessBusiness byId1 = oaProcessBusinessService.getById(oaProcessBusiness.getId());
        byId1.setFsfkch(0);

        byId1.setFcshdb(OaFlowConstant.FLOW_BLZT_YB);
        oaProcessBusinessService.updateById(byId1);
        //提交完成后 更新待办信息
        sysTzMessageService.tjMessage(oaid);
        myLcFlowService.recall(oaid);


        return HttpResult.ok("提交成功");
    }


    /**
     * 批量查询
     */
    @AutoLog(value = "批量查询")
    @ApiOperation(value = "批量查询")
    @ApiImplicitParam(value = "jobids", name = "jobids", required = true)
    @GetMapping(value = "queryBatchJobByJobIds")
    public HttpResult<?> queryBatchJobByJobIds(@RequestParam("jobids")String jobids) {
        if (StringUtils.isBlank(jobids)) {
            return HttpResult.error("参数不能为空,请选择数据");
        }
        List<Map> mapList = new ArrayList<>();
        List<String> jobidsList = Arrays.asList(jobids.split(","));
        if (CollectionUtils.isNotEmpty(jobidsList)) {
            for (String jobid : jobidsList){
                mapList.add(newNkRsJcyBxFlowService.getJobConcernByJobId(jobid));
            }
        }
        return HttpResult.ok(mapList);
    }

    /**
     * 查询当前流程任务详情-通过当前流程任务id
     */
    @AutoLog(value = "查询当前流程任务详情-通过当前流程任务id")
    @ApiOperation(value = "查询当前流程任务详情-通过当前流程任务id")
    @ApiImplicitParam(value = "jobid", name = "流程任务id", required = true)
    @GetMapping(value = "queryJobByJobId")
    public HttpResult<?> queryJobByJobId(@RequestParam("jobid") String jobid) {
        if (StringUtils.isBlank(jobid)) {
            return HttpResult.error("流程任务id不能为空");
        }
        Map map = newNkRsJcyBxFlowService.getJobConcernByJobId(jobid);
        return HttpResult.ok(map);
    }

    /**
     * 获取已办列表-通过当前流程id-自定义退回
     *
     * @param jobid
     * @return
     */
    @AutoLog(value = "获取已办列表-通过当前流程id-自定义退回")
    @ApiOperation(value = "获取已办列表-通过当前流程id-自定义退回")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "jobid", value = "流程任务id"),
            @ApiImplicitParam(name = "isZdhj", value = "是否指定环节，默认：true，指定")
    })
    @GetMapping(value = "queryAllYbJob")
    public HttpResult<?> queryAllYbJob(@RequestParam(value = "jobid", defaultValue = "") String jobid,
                                       @RequestParam(value = "isZdhj", defaultValue = "true") boolean isZdhj) {
        List<Map<String, String>> currentJobPrevious = newNkRsJcyBxFlowService.getCurrentJobPrevious(jobid, isZdhj);
        return HttpResult.ok(currentJobPrevious);
    }

    /**
     * 退回环节
     *
     * @return
     */
    @AutoLog(value = "自定义-退回环节")
    @ApiOperation(value = "自定义-退回环节")
    @PostMapping(value = "returnUpperzdyFlowJob")
    public HttpResult<?> returnUpperzdyFlowJob(@RequestBody OaReturnVo oaReturnVo) {
        newNkRsJcyBxFlowService.returnZdJob(oaReturnVo);
        OaJobEntity oaJobEntity = spcySqsxJobService.getById(oaReturnVo.getJobid());
        OaJobEntity thjob = spcySqsxJobService.getById(oaReturnVo.getShjid());





        OaSpcyJcyFybxd nkRsBxlc = spcyJcyFybxdService.getById(oaJobEntity.getFpid());
        nkRsBxlc.setFbxdzt(OaSpcyJcyFybxdConstant.STATE_HZBD);
        spcyJcyFybxdService.updateById(nkRsBxlc);
        //退回更新通知信息

        String fpid = oaJobEntity.getFpid();
        sysTzMessageService.endMessage(fpid,false,false,true);

        return HttpResult.ok("退回成功");
    }


    /**
     * 批量核销
     */

    @AutoLog(value = "批量核销-通过当前jobid 用逗号隔开")
    @ApiOperation(value = "批量核销-通过当前jobid 用逗号隔开")
    @ApiImplicitParam(value = "jobids", name = "jobids", required = true)
    @GetMapping(value = "allhx")
    public HttpResult<?> allhx(@RequestParam("jobids") String jobids){
        CurrentUserVo currentUser = ThreadContextHelper.getCurrentUser();
        List<String> hxList = Arrays.asList(jobids.split(","));
        for (String hxid:hxList){
            OaJobEntity job = spcySqsxJobService.getById(hxid);
            if (job.getFhjmc().equals(OaFlowConstant.SPCYBXSQLC_HJMC_CNSH)){
                newNkRsJcyBxFlowService.doJobs(hxid, "同意");
            }else {
                return HttpResult.error("批量核销失败，当前数据不是核销环节");
            }

        }
        for (String jobid:hxList){
            List<OaJobEntity> list = spcySqsxJobService.list(new LambdaQueryWrapper<OaJobEntity>().eq(OaJobEntity::getId, jobid));
            for (OaJobEntity lists: list){
                String fpid = lists.getFpid();
                if (StringUtils.isBlank(fpid)){
                    return HttpResult.error("获取不到oaid 请联系管理员");
                }
                OaSpcyJcyFybxd oaSpcyJcyFybxd = spcyJcyFybxdService.getById(lists.getFpid());
                oaSpcyJcyFybxd.setFcnshr(currentUser.getName());
                oaSpcyJcyFybxd.setFcnshsj(new Date());
                spcyJcyFybxdService.updateById(oaSpcyJcyFybxd);
            }
        }
        return HttpResult.ok("批量核销成功");
    }

    /**
     * 财务批量办理
     */
    @AutoLog(value = "财务批量办理报销-通过当前jobid 用逗号隔开")
    @ApiOperation(value = "财务批量办理报销-通过当前jobid 用逗号隔开")
    @ApiImplicitParam(value = "jobids", name = "jobids", required = true)
    @GetMapping(value = "allcw")
    public HttpResult<?> allcw(@RequestParam("jobids") String jobids){
        CurrentUserVo currentUser = ThreadContextHelper.getCurrentUser();
        List<String> hxList = Arrays.asList(jobids.split(","));
        for (String hxid:hxList){
            OaJobEntity job = spcySqsxJobService.getById(hxid);
            if (job.getFhjmc().equals(OaFlowConstant.SPCYBXSQLC_HJMC_CWSH)){
                newNkRsJcyBxFlowService.doJobs(hxid, "同意");
            }else {
                return HttpResult.error("批量审核失败，当前有数据不是财务审核环节");
            }

        }
        for (String jobid:hxList){
            List<OaJobEntity> list = spcySqsxJobService.list(new LambdaQueryWrapper<OaJobEntity>().eq(OaJobEntity::getId, jobid));
            for (OaJobEntity lists: list){
                String fpid = lists.getFpid();
                if (StringUtils.isBlank(fpid)){
                    return HttpResult.error("获取不到oaid 请联系管理员");
                }
                OaSpcyJcyFybxd spcyJcyFybxd = spcyJcyFybxdService.getById(lists.getFpid());
                spcyJcyFybxd.setFcwshr(currentUser.getName());
                spcyJcyFybxd.setFcwshrid(currentUser.getId());
                spcyJcyFybxd.setFcnshsj(new Date());
                spcyJcyFybxdService.updateById(spcyJcyFybxd);
            }
        }
        return HttpResult.ok("批量审核成功");
    }

    /**
     * 中心领导批量办理
     */
    @AutoLog(value = "中心领导批量办理报销-通过当前jobid 用逗号隔开")
    @ApiOperation(value = "中心领导批量办理报销-通过当前jobid 用逗号隔开")
    @ApiImplicitParam(value = "jobids", name = "jobids", required = true)
    @GetMapping(value = "allzxld")
    public HttpResult<?> allzxld(@RequestParam("jobids") String jobids){
        CurrentUserVo currentUser = ThreadContextHelper.getCurrentUser();
        List<String> hxList = Arrays.asList(jobids.split(","));
        for (String hxid:hxList){
            OaJobEntity job = spcySqsxJobService.getById(hxid);
            if (job.getFhjmc().equals(OaFlowConstant.SPCYBXSQLC_HJMC_ZXLDSP)){
                newNkRsJcyBxFlowService.doJobs(hxid, "同意");
            }else {
                return HttpResult.error("批量审核失败，当前数据有不是中心领导审核环节");
            }

        }
        for (String jobid:hxList){
            List<OaJobEntity> list = spcySqsxJobService.list(new LambdaQueryWrapper<OaJobEntity>().eq(OaJobEntity::getId, jobid));
            for (OaJobEntity lists: list){
                String fpid = lists.getFpid();
                if (StringUtils.isBlank(fpid)){
                    return HttpResult.error("获取不到oaid 请联系管理员");
                }
                OaSpcyJcyFybxd spcyJcyFybxd = spcyJcyFybxdService.getById(lists.getFpid());
                spcyJcyFybxd.setFzxspr(currentUser.getName());
                spcyJcyFybxd.setFzxsprid(currentUser.getId());
                spcyJcyFybxd.setFzxspsj(new Date());
                spcyJcyFybxdService.updateById(spcyJcyFybxd);
            }
        }
        return HttpResult.ok("批量审核成功");
    }


    /**
     * 判断当前人是否是中心领导
     */
    @AutoLog(value = "判断当前人是否是中心领导")
    @ApiOperation(value = "判断当前人是否是中心领导")
    @GetMapping(value = "sfzxld")
    public HttpResult<?> sfzxld(){
        CurrentUserVo currentUser = ThreadContextHelper.getCurrentUser();
        //获取中心领导角色
        List<OaSysRole> sysRoles = sysRoleService.list(new LambdaQueryWrapper<OaSysRole>()
                .eq(OaSysRole::getRoleName, NkConstant.ROLE_NAME_ZXZR));
        if (CollectionUtils.isNotEmpty(sysRoles)) {
            List<OaSysUserRole> Cnlist = oaSysRoleMapper.zxzrList();

            if (CollectionUtils.isNotEmpty(Cnlist)){
                String userId = Cnlist.get(0).getUserId();
                if (userId.equals(currentUser.getId())){
                    return HttpResult.ok(true);
                }else {
                    return HttpResult.ok(false);
                }
            }
        }
        return HttpResult.ok(false);

    }

    /**
     * 判断当前人是否具有批量核销权限
     */
    @AutoLog(value = "判断当前人是否具有批量核销权限")
    @ApiOperation(value = "判断当前人是否具有批量核销权限")
    @GetMapping(value = "sfkyhx")
    public HttpResult<?> sfkyhx(){
        CurrentUserVo currentUser = ThreadContextHelper.getCurrentUser();
        //获取出纳角色id
        List<OaSysRole> oaSysRoles = sysRoleService.list(new LambdaQueryWrapper<OaSysRole>()
                .eq(OaSysRole::getRoleName, NkConstant.ROLE_NAME_CN));

        if (CollectionUtils.isNotEmpty(oaSysRoles)) {
            List<OaSysUserRole> Cnlist = oaSysRoleMapper.cnList();

            if (CollectionUtils.isNotEmpty(Cnlist)){
                String userId = Cnlist.get(0).getUserId();
                if (userId.equals(currentUser.getId())){
                    return HttpResult.ok(true);
                }else {
                    return HttpResult.ok(false);
                }
            }
        }
        return HttpResult.ok(false);
    }


    /**
     * 预算指标操作记录，获取报销明细列表
     */
    @AutoLog(value = "预算指标操作记录，获取报销明细列表")
    @ApiOperation(value = "预算指标操作记录，获取报销明细列表")
    @GetMapping(value = "getBxmxList")
    public HttpResult<?> getBxmxList(PageVo pageVo, String bxdgrjlid){
        Page<OaSpcyJcyFybxxmmxjl> getsqmx = newNkRsJcyBxFlowService.getsqmx(pageVo, bxdgrjlid);
        return HttpResult.ok(getsqmx);
    }



    /**
     * 检查员报销当前审批人物姓名和id
     */
    @AutoLog(value = "检查员报销当前审批人物姓名和id")
    @ApiOperation(value = "检查员报销当前审批人物姓名和id")
    @GetMapping(value = "getSpuser")
    public HttpResult<?> getSpuser(String bxdid){
        List slist = new ArrayList<>();
        List xlist = new ArrayList<>();

        OaSpcyJcyFybxd oaSpcyJcyFybxd = spcyJcyFybxdService.getById(bxdid);

        if (oaSpcyJcyFybxd == null){
            throw new BusinessException("获取不到报销单信息");
        }

        //经办人
        String fjbr = oaSpcyJcyFybxd.getFjbr();
        String fjbrid = oaSpcyJcyFybxd.getFjbrid();
        if (StringUtils.isNotBlank(fjbr) && StringUtils.isNotBlank(fjbrid)){
            Map map = new HashMap<>();
            map.put("user",fjbr);
            map.put("userid",fjbrid);
            slist.add(map);
        }

        //部门
        String fjbbmshr = oaSpcyJcyFybxd.getFjbbmshr();
        String fjbbmshrid = oaSpcyJcyFybxd.getFjbbmshrid();
        if (StringUtils.isNotBlank(fjbbmshr) && StringUtils.isNotBlank(fjbbmshrid)){
            Map map = new HashMap<>();
            map.put("user",fjbbmshr);
            map.put("userid",fjbbmshrid);
            slist.add(map);
        }

        //财务
        String fcwshr = oaSpcyJcyFybxd.getFcwshr();
        String fcwshrid = oaSpcyJcyFybxd.getFcwshrid();
        if (StringUtils.isNotBlank(fcwshr) && StringUtils.isNotBlank(fcwshrid)){
            Map map = new HashMap<>();
            map.put("user",fcwshr);
            map.put("userid",fcwshrid);
            slist.add(map);
        }

        //中心领导
        String fzxspr = oaSpcyJcyFybxd.getFzxspr();
        String fzxsprid = oaSpcyJcyFybxd.getFzxsprid();
        if (StringUtils.isNotBlank(fzxspr) && StringUtils.isNotBlank(fzxsprid)){
            Map map = new HashMap<>();
            map.put("user",fzxspr);
            map.put("userid",fzxsprid);
            slist.add(map);
        }

        //核销
        String fcnshr = oaSpcyJcyFybxd.getFcnshr();
        String fcnshrid = oaSpcyJcyFybxd.getFcnshrid();
        if (StringUtils.isNotBlank(fcnshr) && StringUtils.isNotBlank(fcnshrid)){
            Map map = new HashMap<>();
            map.put("user",fcnshr);
            map.put("userid",fcnshrid);
            slist.add(map);
        }

        //根据job获取当前已办的环节 拿到环节的待办人和待办人id
        //按顺序 已办的环节
        List<OaJobEntity> oaJobEntities = spcySqsxJobService.list(new LambdaQueryWrapper<OaJobEntity>()
                .eq(OaJobEntity::getFpid, bxdid)
                .eq(OaJobEntity:: getFblzt, OaCommonFlowConstant.BLZT_YB)
                .orderByAsc(OaJobEntity::getFfssj));

        for (OaJobEntity oaJobEntity : oaJobEntities){
            Map map = new HashMap<>();
            map.put("user", oaJobEntity.getFblr());
            map.put("userid", oaJobEntity.getFydbrid());
            slist.add(map);
        }



        return HttpResult.ok(slist);

    }

    /**
     * 查询中心副主任列表
     */
    @AutoLog(value = "查询中心副主任列表")
    @ApiOperation(value = "查询中心副主任列表")
    @GetMapping(value = "getFzxfzr")
    public HttpResult<?> getFzxfzr(){
        List<YcSysUser> zxzrList = ycSysUserService.list(new LambdaQueryWrapper<YcSysUser>()
                .eq(YcSysUser::getOrgName, OaCommonFlowConstant.ORG_NAME_ZXFZR));

        return HttpResult.ok(zxzrList);
    }

    /**
     * 修改报销单状态
     */
    @AutoLog(value = "修改报销单状态-重新编辑")
    @ApiOperation(value = "修改报销单状态-重新编辑")
    @GetMapping(value = "editBxdzt")
    public HttpResult<?> editBxdzt(String id){
        OaSpcyJcyFybxd oaSpcyJcyFybxd = spcyJcyFybxdService.getById(id);
        oaSpcyJcyFybxd.setFbxdzt(OaSpcyJcyFybxdConstant.STATE_HZBD);
        spcyJcyFybxdService.updateById(oaSpcyJcyFybxd);
        return HttpResult.ok("成功");
    }




}
